import { createRouter, createWebHistory } from 'vue-router'
import GameMenu from '../views/home/GameMenu.vue';
import srGame from '../views/game/srGame.vue';


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: GameMenu,
    },
    {
      path: '/login',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/login.vue'),
    },
    {
      path: '/game-srmode',
      name: 'srGame',
      component: srGame,
    },
  ],
})

router.beforeEach((to, from,next) => {
  const token = sessionStorage.getItem('token');
  console.log(token);
  
  if (to.name !== 'login' && !token) {
    // 将用户重定向到登录页面
    next({ name: 'login' })
  }else{
    next();
  }
})

export default router
